//
//  ZLBackView.swift
//  新浪微博
//
//  Created by 朱玉龙 on 9/24/16.
//  Copyright © 2016 朱玉龙. All rights reserved.
//

import UIKit

@objc protocol ZLBackViewDelegate: NSObjectProtocol {

    func willLogin()
    
    func willRegiser()
    
}


class ZLBackView: UIView {
    
    weak var delegate: ZLBackViewDelegate?
    
    func updateInfo(title:String, circleImageName:String? = "") {
        
        lb_title.text = title
        
        if circleImageName == "" {
            
            animation()
        } else {
            
            iv_cover.isHidden = true
            iv_main.isHidden = true
            iv_circle.image = UIImage.init(named: circleImageName!)
        }
        
    
    }
    
        override init(frame: CGRect) {
    
            super.init(frame: frame)
    
        backgroundColor = UIColor.init(colorLiteralRed: 237.0 / 255, green: 237.0 / 255, blue: 237.0 / 255, alpha: 1)
            
            setMyUI()
        }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    //添加子控件
    private func setMyUI() {
        
        addCircleImageView()
            
            addCoverImageView()
            
            addMainImageView()
        
        addtitleLabel()
        
        addTwoButton()

    }
    
    
    //环
    private  func addCircleImageView() {
        
//        iv_circle.image = UIImage.init(named: "visitordiscover_feed_image_smallicon")
        
        iv_circle.image = UIImage.init(named:"visitordiscover_feed_image_smallicon")

        addSubview(iv_circle)
        
        iv_circle.snp.makeConstraints { (make) in
            
            make.centerX.equalTo(self)
            
            make.centerY.equalTo(self).offset(-80)
            
        }
        
    }
    
    
    
    ///添加环动画
    
    private  func animation() {
        
        let animation = CABasicAnimation.init(keyPath: "transform.rotation")
        
        animation.repeatCount = MAXFLOAT
        
        animation.toValue = M_PI * 2
        
        animation.duration = 15
        
        animation.isRemovedOnCompletion = false
        
        iv_circle.layer.add(animation, forKey: nil)
    }
    
    
    
    //遮罩
    private  func addCoverImageView() {
        
        iv_cover.image = UIImage.init(named: "visitordiscover_feed_mask_smallicon")
        
        addSubview(iv_cover)
        
        iv_cover.snp.makeConstraints { (make) in
            
            make.top.bottom.leading.trailing.equalTo(iv_circle)
        }
        
    }
    
    //中间图片
    
    private  func addMainImageView() {
        
//        iv_main.image = UIImage.init(named: "visitordiscover_feed_image_house")
        iv_main.image = UIImage.init(named:"visitordiscover_feed_image_house")
        
        addSubview(iv_main)
        
        iv_main.snp.makeConstraints { (make) in
            
            make.center.equalTo(iv_circle)
        }
        
        
    }
    
    //文字描述
    
    private  func addtitleLabel() {
        
        addSubview(lb_title)
        
        lb_title.snp.makeConstraints { (make) in
            
            make.top.equalTo(iv_circle.snp.bottom).offset(10)
            
            make.centerX.equalTo(iv_circle)
        }
    }
    
    //按钮
    
    private  func addTwoButton() {
        
        addSubview(btn_login)
        
        addSubview(btn_register)
        
        
        btn_login.snp.makeConstraints { (make) in
            
            make.leading.equalTo(lb_title)
            make.width.equalTo(100)
            make.top.equalTo(lb_title.snp.bottom).offset(20)
        }
        
        
        btn_register.snp.makeConstraints { (make) in
            
            make.trailing.equalTo(lb_title)
            make.width.equalTo(100)
            make.top.equalTo(lb_title.snp.bottom).offset(20)
        }
        
        btn_login.addTarget(self, action: #selector(loginBtnClick), for: .touchUpInside)
        btn_register.addTarget(self, action: #selector(registerBtnClick), for: .touchUpInside)
        
    }
    
    
    func loginBtnClick() {
        
        delegate?.willLogin()
        
    }
    
    func registerBtnClick() {
        
        delegate?.willRegiser()
        
    }
    
    private lazy var iv_circle:UIImageView = UIImageView()
    
    private  lazy var iv_cover:UIImageView = UIImageView()
    
    private lazy var iv_main:UIImageView = UIImageView()
    
    private lazy var lb_title:UILabel = {
        
//        let label = UILabel.init(title: "关注一些人,快回到这里看看有什么惊喜", color: UIColor.darkGray, fontSize: 14)
        
        let label = UILabel.init(title: "关注一些人,快回到这里看看有什么惊喜", color: UIColor.darkGray, fontSize: 14)
        
        label.preferredMaxLayoutWidth = 224
        
        label.numberOfLines = 0;
        
        label.textAlignment = NSTextAlignment.center
        
        return label
    }()
    
    private lazy var btn_login:UIButton = {
        
        let button = UIButton.init(title: "登录", titleColor: UIColor.darkGray, fontSize: 14)
        
        button.setBackgroundImage(UIImage.init(named: "common_button_white"), for: .normal)
        
        return button
    }()
    
    private  lazy var btn_register :UIButton = {
        
        let button = UIButton.init(title: "注册", titleColor: UIColor.orange, fontSize: 14)
        
        button.setBackgroundImage(UIImage.init(named: "common_button_white"), for: .normal)
        
        return button
    }()
    
}

